package com.llamarun;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
 
public class MainThread extends Thread 
{
	private static final String TAG = MainThread.class.getSimpleName();
 	
	private SurfaceHolder surfaceHolder;
	private GameView gameView;
//	private boolean running;
	private Object mPauseLock = new Object();
	private boolean mPaused = false;
	//BackGroundMusic myMusic = new BackGroundMusic(this);
	
	// desired fps
	private final static int MAX_FPS = 15;
	// maximum number of frames allowed to be skipped
	private final static int MAX_FRAME_SKIPS = 5;
	// frame period
	private final static int FRAME_PERIOD = 1000/MAX_FPS;
	
	public MainThread(SurfaceHolder surfaceHolder, GameView gameView) 
	{
		super();
		this.surfaceHolder = surfaceHolder;
		this.gameView = gameView;
		
	}
	
	public void onPause()
	{
		synchronized(mPauseLock)
		{
			mPaused = true;
		}
	}
	
	public void onResume()
	{
		synchronized(mPauseLock)
		{
			mPaused = false;
			mPauseLock.notifyAll();
		}
	}
 	
	@Override
	public void run() 
	{
		Canvas canvas;
		
		long beginTime;         // time when the cycle began
		long timeDiff;          // time to complete the cycle
		int sleepTime;          // extra cycle time
		int framesSkipped;      // number of frames skipped
		
		sleepTime = 0;
		
		Log.d(TAG, "Starting game loop");
		while (true) 
		{
			canvas = null;
			// try locking the canvas for exclusive pixel editing
			// in the surface
			try 
			{
				canvas = this.surfaceHolder.lockCanvas();
				synchronized (surfaceHolder) 
				{
					beginTime = System.currentTimeMillis();
					// reset the number of frames skipped
					framesSkipped = 0;
					this.gameView.update();
					this.gameView.render(canvas);					
					// calculate time of the cycle
					timeDiff = System.currentTimeMillis()-beginTime;
					// calculate sleep time
					sleepTime = (int)(FRAME_PERIOD - timeDiff);
					
					// if sleepTime is >0 we're good to go
					if(sleepTime > 0)
					{
						//Log.d(TAG, "sleep time > 0 :)");
						try
						{
							// send the thread to sleep
							Thread.sleep(sleepTime);
						}
						catch(InterruptedException e){}
					}
					// if sleepTime is <0 we've got to catch up
					while(sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS)
					{
						//Log.d(TAG, "sleep time < 0 :(");
						// update without rendering
						this.gameView.update();
						// add to the sleepTime
						sleepTime += FRAME_PERIOD;
						// add to the frames skipped
						framesSkipped++;
					}
				}
			} 
			finally 
			{
				// in case of an exception the surface is not left in 
				// an inconsistent state
				if (canvas != null) 
				{
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
				// Suspends the thread if the game is paused.
				synchronized(mPauseLock)
				{
					while(mPaused)
					{
						try
						{
							mPauseLock.wait();
						} catch(InterruptedException e){}
					}
				}
			}
		}	
	}
}

